package uk.org.fuzelogik.eldaw.auth;

import java.io.Serializable;


/**
 * Base class for delegated authentication strategy implementations. Provides
 * two convenience methods for executing behavioural logic depending on whether
 * authentication succeeded or not.
 * 
 * @author cgdavies
 */
public abstract class DelegatedAuthenticationStrategy implements
	IDelegatedAuthenticationStrategy, Serializable
{
	private String m_userId;

	final public String getAuthenticatedUserId()
	{
		return m_userId;
	}

	final public boolean isSignedIn()
	{
		return ( m_userId != null );
	}

	final public void setAuthenticatedIdentity( String userId )
	{
		endAuthentication();

		m_userId = userId;

		if( userId != null )
			authenticationSucceeded( userId );
		else
			authenticationFailed();
	}

	/**
	 * Invoked when the third-party gateway indicates that authentication
	 * succeeded.
	 * 
	 * @param userId the identifier the user authenticated as
	 */
	protected void authenticationSucceeded( String userId )
	{

	}

	/**
	 * Invoked when the third-party gateway indicates that authentication
	 * failed.
	 */
	protected void authenticationFailed()
	{

	}

	/**
	 * Invoked just before {@link #authenticationSucceeded(String)} or
	 * {@link #authenticationFailed(String)}. Allows the implementation to free
	 * any resources it's holding on to for the purpose of the authentication
	 * process.
	 */
	protected void endAuthentication()
	{

	}
}
